import * as serviceApi from '@/services/config/currency';

export default {
  namespace: 'currency',

  state: {
    title: '货币',
    list: [],
  },

  effects: {
    *get({ _, callback }, { call, put }) {
      const res = yield call(serviceApi.get);
      yield put({
        type: 'save',
        payload: res,
      });
      if (callback) callback(res);
    },
    *createOrUpdate({ payload, callback }, { call, put }) {
      const res = yield call(payload.id ? serviceApi.update : serviceApi.create, payload);
      yield put({
        type: 'get',
      });
      if (callback) callback(res);
    },
    *remove({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.remove, payload);
      yield put({
        type: 'get',
      });
      if (callback) callback(res);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
  },
};
